package cn.ming.wfchat.app.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.ViewDataBinding;
import cn.ming.wfchat.R;
import cn.wildfirechat.remote.MD5Util;
import cn.ming.wfchat.databinding.DialogMoneyPwdBinding;
import cn.ming.wfchat.kit.dialog.CenterDialog;

/**
 * @author luoming
 * created at 2020-05-09 22:24
 * 付款弹框
 */
public class PaymentDialog extends CenterDialog implements View.OnClickListener {
    private DialogMoneyPwdBinding binding;
    private OnClickListener onClickListener;
    private String addnumber = "";


    /**
     * 定义结果回调接口
     */
    public interface OnClickListener {
        void getPwd(String pwd);


    }

    public PaymentDialog setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }


    @Override
    public int getLayoutId() {
        return R.layout.dialog_money_pwd;
    }

    public static PaymentDialog newInstance(String title, String content) {
        PaymentDialog dialog = new PaymentDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("content", content);
        dialog.setArguments(bundle);
        return dialog;
    }


    @Override
    public void initView(ViewDataBinding dataBinding) {
        binding = (DialogMoneyPwdBinding) dataBinding;
        binding.tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });
        binding.zero.setOnClickListener(this::onClick);
        binding.one.setOnClickListener(this::onClick);
        binding.two.setOnClickListener(this::onClick);
        binding.three.setOnClickListener(this::onClick);
        binding.four.setOnClickListener(this::onClick);
        binding.five.setOnClickListener(this::onClick);
        binding.six.setOnClickListener(this::onClick);
        binding.seven.setOnClickListener(this::onClick);
        binding.eight.setOnClickListener(this::onClick);
        binding.nine.setOnClickListener(this::onClick);
        binding.delete.setOnClickListener(this::onClick);
        binding.tvWangji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Tools.jump(getActivity(), VerificationCodeActivity.class, false);
            }
        });
        String title = getArguments().getString("title");
        String content = getArguments().getString("content");
        if (!TextUtils.isEmpty(title)) {
            binding.tvTitle.setText(title);
        } else {
            binding.tvTitle.setText("请输入密码");
        }
        binding.tvConnect.setText(content);

    }

    @Override
    public boolean isBottom() {
        return true;
    }

    @Override
    public boolean isCancele() {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zero:
                add("0");
                break;
            case R.id.one:
                add("1");
                break;
            case R.id.two:
                add("2");
                break;
            case R.id.three:
                add("3");
                break;
            case R.id.four:
                add("4");
                break;
            case R.id.five:
                add("5");
                break;
            case R.id.six:
                add("6");
                break;
            case R.id.seven:
                add("7");
                break;
            case R.id.eight:
                add("8");
                break;
            case R.id.nine:
                add("9");
                break;
            case R.id.delete:
                delete();
                break;
        }
    }

    public void add(String addStr) {
        addnumber = addnumber + addStr;
        initPassword();
        if (addnumber.length() == 6) {
            dismissAllowingStateLoss();
            onClickListener.getPwd(MD5Util.getMD5String(addnumber));
        }
    }

    public void delete() {
        if (addnumber.length() > 0) {
            addnumber = addnumber.substring(0, addnumber.length() - 1);
            initPassword();
        }
    }

    private void initPassword() {
        binding.tv6.setText("");
        binding.tv5.setText("");
        binding.tv4.setText("");
        binding.tv3.setText("");
        binding.tv2.setText("");
        binding.tv1.setText("");
        switch (addnumber.length()) {
            case 6:
                binding.tv6.setText("●");
            case 5:
                binding.tv5.setText("●");
            case 4:
                binding.tv4.setText("●");
            case 3:
                binding.tv3.setText("●");
            case 2:
                binding.tv2.setText("●");
            case 1:
                binding.tv1.setText("●");
                break;
            default:
                break;
        }
    }

}
